define(['./../initial'], function () {
    var app = angular.module('App.home');
    app.directive('calendar', ['$filter', function ($filter) {
        return {
            restrict: 'AE',
            scope: {
                calendarId: "@",
                options: "=",
                onClick: "=",
                curDate: "@"
            },
            link: function (scope, iElement, iAttrs) {
                //初始化当前选择的日期
                var t = new Date(scope.curDate);
                var curDate = new Date(t.getFullYear(), t.getMonth(), t.getDate());
                var opts = scope.options; //配置选项
                //一个月有多少天
                function getDaysInMonth(month, year) {
                    var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
                    if ((month === 1) && (year % 4 === 0) && ((year % 100 !== 0) || (year % 400 === 0))) {
                        return 29;
                    } else {
                        return daysInMonth[month];
                    }
                }
                var monthNames = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
                scope.dayNames = ['日', '一', '二', '三', '四', '五', '六'];
                scope.month = curDate.getMonth();
                scope.year = curDate.getFullYear();
                scope.date = new Date(scope.year, scope.month, 1);
                scope.dateText = getDateText();

                function getDateText() {
                    return scope.date.getFullYear() + "-" + (monthNames[scope.date.getMonth()]);
                }

                scope.addMonth = function (number) {
                    scope.date.setMonth(scope.date.getMonth() + number);
                    scope.dateText = getDateText();
                    scope.year = scope.date.getYear() + 1900;
                    scope.month = scope.date.getMonth();
                    scope.dayGroups = generateDaysInCalendar();
                };



                var DayItem = (function () {
                    function DayItem(year, month, day) {
                        var t = new Date(year, month, day);
                        this.date = t;
                        this.day = this.date.getDate(), this.year = this.date.getYear() + 1900, this.month = this.date.getMonth();
                        this.dayText = this.day.toString().length < 2 ? '0' + this.day : this.day.toString();
                        this.dayDate = this.date.getDay();
                        this.inMonth = (this.date.getMonth() === scope.month) ? true : false;
                        this.active = (t.getTime() === curDate.getTime()) ? true : false;
                    }
                    DayItem.prototype.addDay = function (dayNumber) {
                        return new DayItem(this.year, this.month, this.day + dayNumber);
                    };
                    return DayItem;
                })();

                function generateDaysInCalendar() {
                    var daysNumber = getDaysInMonth(scope.month, scope.year);
                    var daysList = [];
                    for (var i = 1; i <= daysNumber; i++) {
                        var item = new DayItem(scope.year, scope.month, i);
                        daysList.push(item);
                    }
                    var startDayItem = daysList[0],
                        endDayItem = daysList[daysList.length - 1];

                    //凑齐到周日
                    for (var i = 0; i < startDayItem.dayDate; i++) {
                        var item = startDayItem.addDay(-i - 1);
                        daysList.splice(0, 0, item);
                    }
                    //凑齐到周六
                    for (var i = endDayItem.dayDate + 1; i <= 6; i++) {
                        var item = endDayItem.addDay(i - endDayItem.dayDate);
                        daysList.push(item);
                    }
                    //第一天是周日的话,加上上个月的7天
                    if (daysList.length < 35) {
                        if (startDayItem.dayDate === 0) {
                            for (var i = 0; i < 7; i++) {
                                var item = startDayItem.addDay(-i - 1);
                                daysList.splice(0, 0, item);
                            }
                        }
                    }
                    var dayGroups = [];
                    daysList.forEach(function (item, index) {
                        if (index > 0 && (index + 1) % 7 === 0) {
                            var piece = daysList.slice(index - 6, index + 1);
                            dayGroups.push(piece);
                        }
                    });
                    return dayGroups;
                }
                
                //用户选择了日期
                scope.selectDate = function (dayItem) {
                    scope.dayGroups.forEach(function (piece) {
                        piece.forEach(function (item) {
                            item.active = false;
                        });
                    });
                    dayItem.active = true;
                    curDate = dayItem.date;
                    scope.onClick({
                        date: $filter('date')(dayItem.date, 'yyyy-MM-dd'),
                        calendarId: scope.calendarId
                    });
                };

                scope.dayGroups = generateDaysInCalendar();

            },
            templateUrl: '/partials/calendar.html',
        };
    }])
});
